# -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 of the
#    License, or (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import time
from openerp.osv import osv, fields
from openerp.tools.translate import _


class param_daily_time_sheet_report(osv.osv_memory):
    _name = 'param.daily.time.sheet.report'
    _description = 'Daily Timesheet Report'
    _columns = {
        'project_id': fields.many2one('project.project', 'name'),
        'project_task_ids': fields.many2many('project.task', 'project_task_id_rel', 'project_id', 'task_id', 'Tasks'),
    }

    def daily_timesheet_print2(self, cr, uid, ids, context=None):
        if context is None:
            context = {}
        emp_obj = self.pool.get('hr.employee')
        
        emp_name = ''
        cur_date = False
        
        datas = {'ids': context.get('active_ids', [])}
        datas['model'] = 'param.daily.time.sheet.report'
        datas['form'] = self.read(cr, uid, ids)[0]
        wizard = self.browse(cr, uid, ids[0], context)
        count = 0
        for pt in wizard.project_task_ids:
            count += 1
        if count > 2:
            raise osv.except_osv(_('Invalid action !'), _('You cannot select more than 2 tasks'))
        elif count == 0:
            raise osv.except_osv(_('Invalid action !'), _('Please select at least 1 task'))
        
        emp_id = emp_obj.search(cr, uid, [('user_id', '=', uid)])
        emp_name = emp_obj.browse(cr, uid, emp_id).name or ''
        cur_date = time.strftime("%d/%m/%Y")
        
        return {
            'type': 'ir.actions.report.xml',
            'report_name': 'daily.timesheet.report',
            'name': 'Timesheet (' + emp_name + ' ' + cur_date + ')',
            'datas': datas,
        }
    
    def onchange_project_selection(self, cr, uid, ids, project_id, context=None):
        if context is None:
            context = {}
        
        res = {'value': {'project_task_ids': False}}

        return res
    
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
